package com.sunda.spmsweb.wmscontroller;


import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.IWorkshopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 车间表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-12-15
 */
@RestController
@RequestMapping("/workshop")
@Api(tags = "车间基本信息", description = "车间基本信息接口")
public class WorkshopController {


    @Autowired
    IWorkshopService iWorkshopService;

    @RequestMapping("/getWorkshopList")
    @ApiOperation(value = "获取全部车间信息",notes = "获取全部车间信息", httpMethod = "GET")
    public ResponseResult getWorkshopList(){
        try {
            return ResponseResult.success().add("workshopList", iWorkshopService.getWorkshopList());
        }catch (Exception e){
            return ResponseResult.error("获取车间信息出错");
        }
    }

    @RequestMapping("/createWorkshop")
    @ApiOperation(value = "新建/更新车间信息",
            notes = "新建/更新车间信息\n" +
                    "工厂代码、车间代码、车间中英文都必输。werks + workshopCode 确定唯一数据，如果不存在则新建，存在则更新。", httpMethod = "POST")
    public ResponseResult getOrderWerksByWerks(@RequestParam String werks,
                                               @RequestParam String workshopCode,
                                               @RequestParam String workshopNameZh,
                                               @RequestParam String workshopNameEn,
                                               @RequestParam(required = false) String workshopDesc) {
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWorkshopService.createWorkshop(werks, workshopCode, workshopNameZh, workshopNameEn, workshopDesc, userId);
        } catch (Exception e) {
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }
}
